package com.itheima.test;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class test2 {
    @Autowired
    private UserMapper userMapper;

    @Test
    //List<T> selectList(Wrapper<T> queryWrapper);// 条件查询，返回值为多条记录
    //    T selectOne(Wrapper<T> queryWrapper);// 条件查询，返回值为一条记录
    public void findAll() {
        //条件构造器，封装查询条件，泛型就是要操作的实体类名字
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        List<User> userList = userMapper.selectList(userQueryWrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    //查询条件 age < 10
    @Test

    public void findList() {
        //设置条件,相当于前端传人
        Integer ageParam = 10;
        //条件构造器
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if (ageParam != null) {
            userQueryWrapper.lt("age", ageParam);//age<10
        }
        List<User> userList = userMapper.selectList(userQueryWrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    //    基于Lambda形式
    @Test
    public void findList2() {
        Integer ageParam = 8;
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //参数1 布尔值 只有为true，后面两个参数才能生效
        //参数2 实体类中的属性名（USer：：get属性）
        userLambdaQueryWrapper.gt(ageParam != null, User::getAge, ageParam);
        List<User> userList = userMapper.selectList(userLambdaQueryWrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }
    @Test
    public static void main(String[] args) {
        System.out.println("你好我的盆友");
    }

}
